package fr.syntheses.blog.Service.mailler;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author Olivier Guillou
 * current date
 *
 */
public class MailServices {

	
	String error = "";

	public String sendMail(String formMailAdress, String mdp) {

		System.out.println("==> Entering in mailService : " + mdp + " + "  + formMailAdress);
			
		
		String formMailContenu = "Voici votre mot de passe : "  + mdp;
		String formMailSujet = "Blog Aziz : votre demande de mdp";

		String strHost = "smtp.orange.fr";
		String strFrom = formMailAdress;
		String strTo = formMailAdress;
		String strMailer = "MonMailer";

		try {
			Properties pProps = System.getProperties ();

			pProps.put ("mail.smtp.host", strHost);

			Session sSession = Session.getDefaultInstance (pProps,null);

			Message msgMessage = new MimeMessage (sSession);

			msgMessage.setFrom (new InternetAddress (strFrom));

			msgMessage.setRecipients (Message.RecipientType.TO, InternetAddress.parse (strTo, false));

			msgMessage.setSubject (formMailSujet);

			msgMessage.setText (formMailContenu);

			msgMessage.setHeader ("X-Mailer",strMailer);

			msgMessage.setSentDate (new Date ());

			Transport.send (msgMessage);

			System.out.println ("Message envoy�");

		} catch (Exception e) {

			System.out.println ("Erreur : " +e.getMessage ());

			error = e.getMessage ();

		}
		
		return error;

	}

}
